package cst.neu.yuejian_31.model;

import cst.neu.yuejian_31.bean.ClassStateBean;
import cst.neu.yuejian_31.iface.ClassCollectStateIface;
import cst.neu.yuejian_31.iface.ClassCollectStateListener;
import cst.neu.yuejian_31.service.ClassCollecStateService;
import cst.neu.yuejian_31.Common.Constant;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by xxe on 2017/10/25.
 */

public class ClassCollectStateModel implements ClassCollectStateIface {
    private Retrofit retrofit;
    public ClassCollectStateModel()
    {
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }    @Override
    public void getClassState(String userId, String classId, final ClassCollectStateListener classCollectStateListener) {
        final ClassCollecStateService classCollecStateService=retrofit.create(ClassCollecStateService.class);
        Call<ClassStateBean>call=
                classCollecStateService.getClassState(userId,classId);
        call.enqueue(new Callback<ClassStateBean>() {
            @Override
            public void onResponse(Call<ClassStateBean> call, Response<ClassStateBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    classCollectStateListener.onSuccess(response.body());
                }else {
                    classCollectStateListener.onFail("Fail");
                }
            }

            @Override
            public void onFailure(Call<ClassStateBean> call, Throwable t) {
                   classCollectStateListener.onFail(t.toString());
            }
        });

    }
}
